
from random import random
from pandac.PandaModules import *
from direct.actor import Actor
from direct.interval.IntervalGlobal import * 

from direct.particles.Particles import Particles 
from direct.particles.ParticleEffect import ParticleEffect 
import math
import random
from panda3d.ai import *

import globals 

class FriendMidi:
    def __init__(self):
        self.model = None
        self.time = None
        self.startPos = None
        self.stageBelong = None
        self.points = []
        self.times = []
        self.sound = None
        self.pos = None
        self.dead = False
        self.scorePoints = 0
        self.actor = Actor.Actor();
        self.node = render.attachNewNode("Friend")
        self.loop = False
        self.duration = 0
        self.endPos = None
        self.channel = 0
        self.note = 0
        self.volume = 0
        self.key = 0
        self.anim = None
        self.cNodePath = None
        self.type = "NORMAL"
        self.offset = 0.0
        self.snd = None
        self.coltype = ""
        self.colx = None
        self.coly = None
        self.colz = None

    def init(self):
        taskMgr.doMethodLater(self.time - self.offset, self._init, 'Start-Sooner-Friend')

    def _init(self, task):
        if globals.NUMBERENEMIES >= globals.NUM_MAX_NUMBERENEMIES:
            del self
            return task.done

        globals.NUMBERENEMIES += 1

        if self.anim != "":
            self.actor.loadModel(self.model)
            self.actor.loadAnims({"Run":self.anim})
            self.actor.bindAnim('Run')
            self.actor.loop('Run')
            self.actor.setPlayRate(1.5 + (globals.RATE - 1), 'Run')
            self.actor.reparentTo(self.node)
        else:
            dummynode = loader.loadModel(self.model)
            dummynode.reparentTo(self.actor)
            self.actor.reparentTo(self.node)
            self.actor.setHpr(90,0,0)
            self.actor.setLightOff()

        #Create a collision solid for this model
        self.cNode = CollisionNode('friendMidiCol')
        self.cNode.setIntoCollideMask(BitMask32.bit(1))
        self.cNode.setFromCollideMask(BitMask32.allOff())
        if self.coltype == "BOX":
            self.cNode.addSolid(CollisionBox(Point3(0.0,0.0,0.0),self.colx,self.coly,self.colz))
        elif self.coltype == "SPHERE":
            self.cNode.addSolid(CollisionSphere(0, 0, 0.0, self.colx))
        elif self.coltype == "BOX_OFFSET":
            self.cNode.addSolid(CollisionBox(Point3(0.0,60.0,0.0),self.colx,self.coly,self.colz))
        #elif self.coltype == "POLYSET":
        #    self.cNodePath = self.actor.find("**/central")
        self.cNodePath = self.actor.attachNewNode(self.cNode)
  
        self.actor.setColor(1.0,1.0,1.0,1.0)
        self.actor.setScale(1.0,1.0,1.0)

        self.duration = globals.TEMPO / 60.0
        self.startx = 4000
        self.costime = math.cos(self.time)
        self.sintime = math.sin(self.time)
        self.mySeq = Sequence()
        self.startPos = Point3(0,0,0)

        if self.type == "VISIBILITY":
            #print str((self.pitch-48)*10.16)
            self.startPos = Point3(self.startx,((self.pitch-48)*10.16),0)
            int2 = LerpPosInterval(self.node,self.duration ,Point3(globals.PlayerPosition.getX(),self.startPos.getY(),self.startPos.getZ()))
            int3 = LerpPosInterval(self.node,self.duration , -self.startPos)
            self.mySeq.append(int2)
            self.mySeq.append(int3)

        elif self.type == "GIVESHOT":
            self.startPos = Point3(self.startx,0,((self.pitch-48)*10.16))
            int2 = LerpPosInterval(self.node,self.duration ,Point3(globals.PlayerPosition.getX(),self.startPos.getY(),self.startPos.getZ()))
            int3 = LerpPosInterval(self.node,self.duration , -self.startPos)
            self.mySeq.append(int2)
            self.mySeq.append(int3)

        self.parallel = Sequence(self.mySeq)

        if self.loop:
            self.parallel.loop()
        else:
            self.parallel.start()

        self.node.setPos(self.startPos)
        self.updateTask = taskMgr.add(self._update, 'Friend-Update')
        return task.done


    def _update(self, task):
        dt = globalClock.getDt()
        if( dt > .20):
            return task.cont

        if  task.time > self.duration+0.5 or not self.parallel.isPlaying() or self.dead == True:
            self.parallel.finish()
            self.node.removeNode()

            if self.dead == True and self.sound != "" and self.snd:
                self.snd.play()

            globals.NUMBERENEMIES -= 1
            del self
            return task.done
        return task.cont

    def die(self,player):
        if self.type == "GIVESHOT":
            if player.shot_count < 5:
                player.shot_count += 1
        elif self.type == "VISIBILITY":
            if player.light_count < 1.0:
                player.light_count += 0.2;
                player.restartLight()

        globals.SCORE += self.scorePoints
        self.dead = True
